CtlHTMLPartialOwnerDraw

The CtlHTMLPartialOwnerDraw function provides the ability to change the way command buttons, check boxes, and radio button controls are displayed.

typedef BOOL (*PFNPartialOwnerDraw)(
  HWND hWnd,                       // handle to the control
  HDC hDC,                         // handle to the control device context
  LPSTR szText,                    // pointer to the control text
  RECT * pClientRect,              // pointer to the control client rectangle
  RECT * pTextRect,                // pointer to the text rectangle
  int textStyle,                   // the text style
  BOOL isPushButton,               // if TRUE, control is a push button
  BOOL hasFocus,                   // if TRUE, control has focus
  BOOL isDown,                     // if TRUE, control push button is down
  BOOL calcSize                    // if TRUE, the control size is being calculated
);
void CtlHTMLPartialOwnerDraw(
  PFNPartialOwnerDraw partialOwnerDraw // pointer to the ownerdraw callback function
);

Parameters

hWnd
Handle to the control.

hDC
Handle to the control device context.

szText
Pointer to the control text. You can change this text.

pClientRect
Pointer to the entire control client rectangle.

pTextRect
Pointer to the control rectangle where the text can be drawn. This rectangle doesn't include the button edge for command buttons or the check or radio graphic for check boxes and radio buttons.

textStyle
The style of the text to be drawn. Uses the same styles as DrawText, such as DT_WORDBREAK and DT_CENTER.

isPushButton
If TRUE, control is a push button.

hasFocus
If TRUE, control has focus.

isDown
If TRUE, control push button is down.

calcSize
If TRUE, the control size is being calculated but the control is not being drawn. If FALSE, the control is being drawn. By handling the case when this value is TRUE, you can change the size of a control or its text.

Return Values

The callback function returns TRUE if the text is to be drawn, otherwise it returns FALSE.

Description

Call the CtlHTMLPartialOwnerDraw function to set a callback function to change the way command buttons, check boxes, and radio button controls are displayed. A single callback function is used for all controls, so you need to determine the controls you wish to ownerdraw from the input parameters, typically by using GetDlgCtrlID API function. For example with MFC:


// Draws radio buttons using colors instead of text. The control text contains the RGB color value.
BOOL MyPartialOwnerDraw (HWND hWnd, HDC hDC, LPSTR szText, RECT *pClientRect, RECT *pTextRect, 
                         int textStyle, BOOL isPushButton, BOOL hasFocus, BOOL isDown, BOOL calcSize)
{
   int id;

   id = GetDlgCtrlID(hWnd);
   if (id == IDC_OWNERDRAWRB1 || id == IDC_OWNERDRAWRB2 || id == IDC_OWNERDRAWRB3)
   {
      // in this case, the ownerdraw doesn't change the size
      if (calcSize)
         return TRUE;

      COLORREF color;
      HBRUSH   hBrush, hOldBrush;
      HPEN     hPen,   hOldPen;

      // set the brush and pen
      if (IsWindowEnabled(hWnd))
      {
         sscanf(szText, "%x", &color);
         hBrush = CreateSolidBrush(color);
      }
      else
         hBrush = GetSysColorBrush(COLOR_GRAYTEXT); // use same color as disabled text
      hPen      = (HPEN)GetStockObject(BLACK_PEN);
      hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
      hOldPen   = (HPEN)SelectObject(hDC, hPen);

      // do drawing
      if (isPushButton)
      {
         if (isDown)
            Rectangle(hDC, pTextRect->left + 5, pTextRect->top + 5, pTextRect->right - 3, pTextRect->bottom - 3);
         else
            Rectangle(hDC, pTextRect->left + 4, pTextRect->top + 4, pTextRect->right - 4, pTextRect->bottom - 4);
      }
      else
         Rectangle(hDC, pTextRect->left, pTextRect->top, pTextRect->right, pTextRect->bottom);

      // be sure to restore all GDI objects
      SelectObject(hDC, hOldBrush);
      SelectObject(hDC, hOldPen);

      // draw the focus rect
      if (hasFocus)
      {
         RECT     focusRect;

         focusRect = *pTextRect;
         InflateRect(&focusRect, -2, -2);
         DrawFocusRect(hDC, &focusRect);
      }

      // return TRUE to not draw the text
      return TRUE;
   }
   else
      return FALSE;
}

You can use partial ownerdraw to change practically any attribute of the control's appearance, such as the control size and text attributes (such as the text color and background color), as well as drawing icons or bitmaps to the control.

Copyright ⌐ 1999, Windmill Point Software. All Rights Reserved.
Last Updated May 19, 1999